package com.qf.zyh.web.controller;

import com.qf.zyh.common.Const;
import com.qf.zyh.entity.Address;
import com.qf.zyh.entity.User;
import com.qf.zyh.services.Impl.UerServicesImpl;
import com.qf.zyh.services.UserServices;
import com.qf.zyh.util.Base64Utils;
import com.qf.zyh.util.MD5Utils;
import com.qf.zyh.util.RandomUtils;
import com.qf.zyh.util.StrUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user")
public class UserController extends BaseServlet {
    UserServices us = new UerServicesImpl();

    public String checkname(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String uname = request.getParameter("username");
        //System.out.println(uname);
        User user = us.selectName(uname);
        //System.out.println(user);
        if (user != null) {
            return Const.SUCCESS;
        }
        return Const.FAIL;
    }

    public String register(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String gender = request.getParameter("gender");
        String email = request.getParameter("email");
        User user = new User(username, MD5Utils.md5(password), email, gender, 0, RandomUtils.createActive(), 0);

        boolean b = us.addUser(user);
        if (b) {
            return Const.SysConst.REDIRECT + "index.jsp";
        } else {
            request.setAttribute("msg","注册失败，请重新再试");
            return Const.SysConst.FORWARD + "register.jsp";
        }
    }

    //激活界面点击后触发，收集到code和emil查找对应数据，然后修改激活属性
    public String activate(HttpServletRequest request, HttpServletResponse response) {
        //获取code
        String c = request.getParameter("c");
        //获取emil
        String e = request.getParameter("e");
        //返回的是字符串
        String u = us.activate(c,e);
        request.setAttribute("msg",u);
        return Const.SysConst.FORWARD+"message.jsp";
    }

    public String login(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String auto = request.getParameter("auto");
        User uu = us.login(username,password);
        if (uu!=null) {
            request.getSession().setAttribute(Const.UserCont.IS_LOGIN,uu);
            if (StrUtils.empty(auto)) {
                //存储账号密码
                String np = username+":"+password;
                //创建cookie，将加密后的账号密码存储其中
                Cookie ck = new Cookie(Const.UserCont.AUTO_LOGIN, Base64Utils.encode(np));
                //设置时间s
                ck.setMaxAge(60*60*24*14);
                //设置上下文路径/为全局路径
                ck.setPath("/");
                //添加cookie
                response.addCookie(ck);
            }
            return Const.SysConst.REDIRECT+"index.jsp";
        }else {
            request.setAttribute("msg","登录失败");
            return Const.SysConst.FORWARD+"login.jsp";
        }
    }

    public String logOut(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().invalidate();
        //清除cook是覆盖cook信息为空并设置时间为0
        Cookie ck = new Cookie(Const.UserCont.AUTO_LOGIN,"");
        ck.setMaxAge(0);
        //切记设置路径，路径不同只能新建一个cookie无法修改
        ck.setPath("/");
        response.addCookie(ck);
        return Const.SysConst.REDIRECT+"index.jsp";
    }
}
